package com.xayy.health.mgt.service.impl;

import cn.kawins.base.result.CheckResult;
import cn.kawins.base.utils.BeanUtil;
import cn.kawins.base.utils.CollUtil;
import cn.kawins.mybatis.base.BaseServiceImpl;
import com.xayy.health.mgt.model.SatelliteCode;
import com.xayy.health.mgt.service.SatelliteCodeService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author Nie
 * @version V1.0
 * @date 2023/5/31 9:59
 */
@Service
public class SatelliteCodeServiceImpl extends BaseServiceImpl<SatelliteCode> implements SatelliteCodeService {
    /**
     * 新增卫星节点信息
     *
     * @param satelliteCode 卫星信息
     * @return 结果日志
     */
    @Override
    public CheckResult<Object> insertSatelliteCode(SatelliteCode satelliteCode) {
        List<SatelliteCode> select = select(SatelliteCode.builder().satelliteCode(satelliteCode.getSatelliteCode()).build());
        if (CollUtil.isNotEmpty(select)) {
            return CheckResult.fail("当前前卫星Code：" + satelliteCode.getSatelliteCode() + "已经在库中配置");
        }
        satelliteCode.setId(null);
        insert(satelliteCode);
        return CheckResult.success();
    }

    /**
     * 删除卫星节点信息
     * @param id 卫星id
     * @return 结果日志
     */
    @Override
    public CheckResult<Object> deleteSatelliteCode(Long id) {
        deleteById(id);
        return CheckResult.success();
    }

    /**
     * 修改卫星节点信息
     * @param satelliteCode  入参信息
     * @return 结果日志
     */
    @Override
    public CheckResult<Object> updateSatelliteCode(SatelliteCode satelliteCode) {
        Example example = new Example(SatelliteCode.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("satelliteCode", satelliteCode.getSatelliteCode());
        criteria.andNotEqualTo("id", satelliteCode.getId());
        List<SatelliteCode> satelliteCodes = selectByExample(example);
        if (CollUtil.isNotEmpty(satelliteCodes)) {
            return CheckResult.fail("当前前卫星Code：" + satelliteCode.getSatelliteCode() + "已经在库中配置");
        }
        updateById(satelliteCode);
        return CheckResult.success(satelliteCode);
    }

    /**
     * 查询卫星节点信息
     * @return 结果日志
     */
    @Override
    public CheckResult<Object> selectSatelliteCode() {
        List<SatelliteCode> satelliteCodes = selectAll();
        return CheckResult.success(satelliteCodes);
    }

    /**
     * 查询卫星信息
     * @param code  卫星code
     * @return 结果日志
     */
    @Override
    public CheckResult<String> getSatIdByCode(String code) {
        SatelliteCode satelliteCode = selectOne(SatelliteCode.builder().satelliteCode(code).build());
        if (BeanUtil.isEmpty(satelliteCode)) {
            return CheckResult.fail("当前卫星没有配置卫星代号对应关系");
        }
        return CheckResult.success(satelliteCode.getSatelliteId());
    }
}
